﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QMetaType>
#include "ui_plcdl.h"
#include "ui_productdlg.h"
#include "plcdl.h"
#include "cameradlg.h"
#include "detectiondlg.h"
#include "logindlg.h"
#include "productdlg.h"

#include "VimbaApi/ApiController.h"
#include "halconcpp/HalconCpp.h"

#include "Thread/camerathread.h"
#include "Thread/algthread.h"
#include "Thread/plcthread.h"
#include "Thread/csvthread.h"
#include "Thread/saveimgthread.h"
#include <QTime>


using AVT::VmbAPI::Examples::ApiController;
using AVT::VmbAPI::FramePtr;
using AVT::VmbAPI::CameraPtrVector;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


public:
    /* Main function */
    void setInterface(); //界面设置
    void Initialize();   //初始化


    void ShowSwitchBt();

    void AlgSwitchBt();
    void CreateCameraSaveFile();
    // Queries and lists all known camera
    void UpdateCameraListBox();
    // Prints out some logging
    void Log(std::string strMsg, VmbErrorType eErr);
    void Log(std::string strMsg);
    // 切换 自动手动模式
    void SetAlgModel();
    // 绑定显示窗口
    void SetHalconWindows();
    // 数据清零
    void ClearCameraData();
    // UI 显示
    void ShowUI();
    // 获取当前时间
    QString GetTime();
    // 设置时钟
    void SetClock();
    // 保存瞬时数据
    void SaveToIni();

    void RdManufactureIni();

    // 运行时长
    void SetRunTime();

    void SendPlcResult(int result);

    // 重写虚函数: changeEvent  解决最小化 会导致ui界面 假死
    void changeEvent(QEvent *event);
    // 事件处理： 进行相机放大缩小
    bool eventFilter(QObject *obj, QEvent *event);

    /* Camera function */
    void OpenHalconWindows();
    void CloseHalconWindows();
    // 获取相机参数
    void RdDlgCameraInfo(CameraDlgInfo cameraInfo);
    // 写入相机参数
    void WdDlgCameraInfo(CameraDlgInfo cameraInfo);
    // 改变相机采集模式 为自由模式
    void StartDlgConsecutive(CameraDlgInfo cameraInfo);
    // 单张硬触发
    void HardToTrigger(CameraDlgInfo cameraInfo);



    /* Detection function*/
    // 根据数据进行设定ui
    void SetDetectionDlgUI(DetectionDlgInfo detectionInfo);
    // 读取配置文件
    void RdConfigIni();



    void RdProductIni(QString iniFilePath);

    void DirFile(QString iniFilePath);

    void SearchProFiles();
    // 修改ini
    void WriteIni(DetectionDlgInfo detectionInfo);


    // COMBOBOX设置
    void SetComBoBox();
    // 相机参数赋值
    void SetCameraParameter();
    /* PLC function */
    void GetPlcState();
    void IsPlcConnect();

    // 算法规则 0 OK  <1> NG1  <2> NG2
    void AlgRule(int nIndex);

public:
    Ui::MainWindow *ui;
    // 速率
    QString strRev = "0";

    // 窗口打开是否？
    int m_nOpenHalcon = 0; // 0 未打开  1打开

    int bProSave = false;   // 使瞬时产品得以调用
    QString m_strName;

    // Dlg
    PlcDl* m_dPlc;
    CameraDlg* m_dCamera;
    DetectionDlg* m_dDetection;
    LoginDLg* m_dLogin;
    ProductDlg* m_dProduct;

    // save img Thread
    SaveImgThread* m_tSaveImg1;
    SaveImgThread* m_tSaveImg2;
    SaveImgThread* m_tSaveImg3;
    SaveImgThread* m_tSaveImg4;

    // Camera Thread
    CameraThread* m_tCamera1;
    CameraThread* m_tCamera2;
    CameraThread* m_tCamera3;
    CameraThread* m_tCamera4;

    // Alg Thread
    AlgThread* m_tAlg1;
    AlgThread* m_tAlg2;
    AlgThread* m_tAlg3;
    AlgThread* m_tAlg4;

    // Plc Thread
    PlcThread* m_tPlc;

    // Csv Thread
    CsvThread* m_tCsv;
    CsvData m_csData;
    // Halcon
    Hlong labe_1;
    Hlong labe_2;
    Hlong labe_3;
    Hlong labe_4;
    Hlong big_labe;


    // 主界面 hv_WindowHandle
    bool m_bStart = false;
    int m_nOldTime = 0;

    HalconCpp::HTuple m_hvMainHandle1;
    HalconCpp::HTuple m_hvMainHandle2;
    HalconCpp::HTuple m_hvMainHandle3;
    HalconCpp::HTuple m_hvMainHandle4;

    HalconCpp::HTuple m_hvBigMainHandle;
    // Our controller that wraps API access
    ApiController m_ApiController;
    // A list of known camera IDs

    //  配置文件参数
    std::vector<std::string> m_cameras;
    std::vector<QString> m_products;

    DetectionDlgInfo m_detection;

    QStringList file_list;  // 配置文件列表
    QStringList file_names; // 文件名
    int m_nProductNum = 0;
    // login 登录解锁
    bool m_bAdmin = false;

    // 相机是否 有缺失 或断开
    bool m_bIsCamera;
    //plcInfo
    PlcInfo nPlcInfo;
    // 相机计算数据
    int m_nCameraResult = 0;  // 总量
    int m_nDelet = 0;  // 删除图片数据
    int m_nNG1 = 0;
    int m_nNG2 = 0;
    int m_nNG3 = 0;
    int m_nOK = 0;
    int m_nIdex = 0; // PLC 通信计数
    int m_nResult = 0; // 速率
    int m_nTime = 0;

    // Alg Switch
    int m_nAlgSw1 = 1;
    int m_nAlgSw2 = 1;
    int m_nAlgSw3 = 1;
    int m_nAlgSw4 = 1;

    QString m_sRunTime;

    int m_nSS = 0;
    int m_nMM = 0;
    int m_nHH = 0;

    int m_nDay = 0;
    QTimer *myTimer;
    // main 主界面显示数据
    CameraInfo m_nCameraInfo1;
    CameraInfo m_nCameraInfo2;
    CameraInfo m_nCameraInfo3;
    CameraInfo m_nCameraInfo4;

private slots:
    void on_switchBt_3_clicked();
    void on_switchBt_2_clicked();


    void on_rightButton_1_1_clicked();
    void on_rightButton_1_2_clicked();

    void on_rightButton_1_4_clicked();

    void systimer();
    void on_rightButton_1_3_clicked();

    void on_rightButton_1_5_clicked();

    void on_clearBt_clicked();
    void on_btCmSav_1_clicked();

    void on_btCmSav_2_clicked();

    void on_btCmSav_3_clicked();

    void on_btCmSav_4_clicked();

    void on_comboBox_currentIndexChanged(int index);

    void on_btCmAlg_1_clicked();

    void on_btCmAlg_2_clicked();

    void on_btCmAlg_3_clicked();

    void on_btCmAlg_4_clicked();

    void on_OKAndNGBt_clicked();

public slots:
    // Button
    void on_startButton_clicked();

    void on_stopButton_clicked();

    void on_minButton_clicked();

    void on_quiteButton_clicked();

    void on_switchBt_4_clicked();

    // The event handler for the camera list changed event
    void DlgListChanged(int Index);
    void OnCameraListChanged(int reason);
    void CameraResult(int m_nResult);

    /* Camera Dlg */
    void ReceiveCameraDlgInfo(CameraDlgInfo cameraInfo);
    /* Detection Dlg */
    void ReceiveDetecDlgInfo(DetectionDlgInfo detectionInfo);
    /* Plc Dlg */
    void RdPlcInfo(SIG_PLC nIndex);
    void WdPlcInfo(SIG_PLC nIndex);
    /* Login Dlg */
    void ReceiveLoginDlgInfo(int nIndex);

    /* Pro Dlg */
    void ReceiveProDlgInfo(int nIndex);
private:
    int m_nBtrightButton_1_1 = 0;
    int m_nBtrightButton_1_2 = 0;
    int m_nBtrightButton_1_3 = 0;
    int m_nBtrightButton_1_4 = 0;



    int m_nOKAndNG = 0; //0 NG  1 异常  2 All
};

#endif // MAINWINDOW_H
